package com.lehe.controller.admin;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lehe.common.utils.StringUtil;
import com.lehe.dao.UserInfoDao;
import com.lehe.entry.AdminUserDto;
import com.lehe.entry.AdminUserSessionDto;
import com.lehe.service.impl.AdminMenuService;
import com.lehe.service.impl.AdminRoleService;
import com.lehe.service.impl.AdminUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lehe.common.Contants;
import com.lehe.entry.BaseResponse;
import com.lehe.util.VerifyCodeUtils;

@Controller
@RequestMapping("login")
public class LoginController {

	@Autowired
	AdminUserService adminUserService;
	@Autowired
	AdminRoleService adminRoleService;
	@Autowired
	AdminMenuService adminMenuService;
	@Autowired
	UserInfoDao userInfoDao;

	@RequestMapping
	public String index(){
		return "admin/login/login";
	}
	
	@RequestMapping("verifyCode")
	public String verifyCode(HttpServletResponse response,HttpServletRequest request){
		response.setHeader("Pragma", "No-cache");  
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0);  
        response.setContentType("image/jpeg");  
          
        //生成随机字串  
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);  
        //存入会话session  
        HttpSession session = request.getSession(true);  
        session.setAttribute("rand", verifyCode.toLowerCase());  
        //生成图片  
        int w = 100, h = 34;  
        try {
			VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
		} catch (IOException e) {
		}  
		return null;
	}
	
	
	@RequestMapping("doLogin")
	@ResponseBody
	public BaseResponse doLogin(String userName,String psw,String code,HttpServletRequest request){
		Object serverCode = request.getSession().getAttribute("rand");
		if(StringUtil.isBlank(userName) || StringUtil.isBlank(psw)){
			return new BaseResponse("002","用户名和密码不能为空");
		}
		if(serverCode == null || !serverCode.equals(code)){
			return new BaseResponse("001","验证码不正确");
		}
		AdminUserDto adminUserDto = adminUserService.queryByAccount(userName);
		if(null == adminUserDto){
			return new BaseResponse("003","用户不存在");
		}
		if(!Contants.ADMIN_USER_STATUS_ACTIVE.equals(adminUserDto.getStatus())){
			return new BaseResponse("004","用户已禁用");
		}
		if(!DigestUtils.md5Hex(psw).equals(adminUserDto.getPwd())){
			return new BaseResponse("005","用户名或密码不匹配");
		}
		BaseResponse resp = new BaseResponse("1","成功");
		AdminUserSessionDto sessionDto = new AdminUserSessionDto();
		BeanUtils.copyProperties(adminUserDto, sessionDto);
		String[] ids = adminRoleService.queryById(sessionDto.getRoleId()).getMenuIds().split(",");
		sessionDto.setMenuDtoList(adminMenuService.queryMenusByIds(ids));
		if(StringUtil.isNotBlank(sessionDto.getUserIds())){
			sessionDto.setUserInfoDtoList(userInfoDao.queryUserInfos(Arrays.asList(sessionDto.getUserIds().split(","))));
		}
		request.getSession().setAttribute(Contants.SESSION_ADMIN_USER_KEY, sessionDto);
		return resp;
	}

	@RequestMapping("logout")
	public String logout(HttpServletRequest request){
		request.getSession().removeAttribute(Contants.SESSION_ADMIN_USER_KEY);
		return "admin/login/login";
	}

	@RequestMapping("loginTestMode")
	public String loginTestMode(HttpServletRequest request){
		AdminUserDto adminUserDto = new AdminUserDto();
		adminUserDto.setAccount("test");
		adminUserDto.setName("测试模式");
		adminUserDto.setRoleId(1L);
		adminUserDto.setRoleName("管理员");
		AdminUserSessionDto sessionDto = new AdminUserSessionDto();
		BeanUtils.copyProperties(adminUserDto, sessionDto);
		String[] ids = adminRoleService.queryById(sessionDto.getRoleId()).getMenuIds().split(",");
		sessionDto.setMenuDtoList(adminMenuService.queryMenusByIds(ids));
		request.getSession().setAttribute(Contants.SESSION_ADMIN_USER_KEY, sessionDto);
		return "redirect:/admin";
	}

}
